package com.estate.service.controller;

import com.estate.bean.Letter;
import com.estate.service.service.LetterService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/letter")
public class LetterController {

    @Resource
    private LetterService letterService;

    @GetMapping("/list/{page}/{size}")
    JsonResult list(@PathVariable("page") int page, @PathVariable("size") int size) {
        log.info("page:{},size:{}", page, size);
        JsonResult result = letterService.listByPage(page, size);
        log.info("result:{}", result);
        return result;
    }

    @PostMapping("/save")
    JsonResult save(Letter letter) {
        log.info("新增letter{}", letter);
        JsonResult result = letterService.saveLetter(letter);
        log.info("result:{}", result);
        return result;
    }

    @DeleteMapping("/delete")
    JsonResult delete(@RequestParam("letterId") String letterId) {
        log.info("删除的letterId{}", letterId);
        JsonResult result = letterService.deleteLetter(letterId);
        log.info("result:{}", result);
        return result;
    }

    @PutMapping("/update")
    JsonResult update(Letter letter) {
        log.info("修改letter{}", letter);
        JsonResult result = letterService.updateLetter(letter);
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/list/{letterId}")
    public JsonResult list(@PathVariable("letterId") String letterId){
        log.info("letterId:{}", letterId);
        JsonResult result = ResultTool.success(letterService.getById(letterId));
        log.info("result:{}", result);
        return result;
    }
}
